package com.fss.user.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fssDataModels.TbUser;
import com.fss.common.dao.CommonDao;
import com.fss.user.dao.UserDao;
import com.opensymphony.xwork2.ActionContext;

public class UserServiceImpl implements UserService{
	private UserDao userDao; 
	private CommonDao commonDao;

	@Override
	public int addUser(TbUser user) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(new Date());
		user.setLastLogin(dateStr);
		user.setCreatedTime(new Date());
		int rtn_num = this.userDao.insertUser(user);
		if(rtn_num == 1)
			ActionContext.getContext().getSession().put("user_login", user);
		return rtn_num;
	}

	@Override
	public int editUser(TbUser user) {
		//user.setLastLogin();
		int rtn_num = this.userDao.updateUser(user);
		if(rtn_num == 1)
			ActionContext.getContext().getSession().put("user_login", user);
		return rtn_num;
	}

	@Override
	public int deleteUser(String userId) {
		int rtn_num = this.userDao.deleteUser(this.getUserById(userId));
		return rtn_num;
		
	}
	
	

	@Override
	public int deleteAllUser(String[] userId) throws Exception{
		try{
			for(int i=0;i<userId.length;i++){
				if(userId[i]!=""){
					this.userDao.deleteUser(this.getUserById(userId[i]));
				}
			}
		}catch (Throwable ex){
			return 0;
		}
		
		return 1;
		
	}

	@Override
	public List<TbUser> getUsersByDate(Date toDate) {
		
		List users = this.commonDao.getUsers(null, toDate);
		return users;
	}

	@Override
	public TbUser getUser(String userId, String psw) {
		TbUser user=this.userDao.getUser(userId);
		if(user==null||!psw.equalsIgnoreCase(user.getPwd().trim())){
			return null;
		}else{			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String dateStr = sdf.format(new Date());
			
			user.setLastLogin(dateStr);
			this.editUser(user);
			ActionContext.getContext().getSession().put("user_login", user);
			
			return user;
		}

	}
	
	@Override
	public int addContect(String contactId) {
		TbUser contactUser = this.userDao.getUser(contactId);
		
		Map session = ActionContext.getContext().getSession();
		TbUser user_login = (TbUser) session.get("user_login");
		
		return this.userDao.addContact(user_login, contactUser);
	}
	
	@Override
	public int deleteContact(String contactId) {
        TbUser contactUser = this.userDao.getUser(contactId);
		
		Map session = ActionContext.getContext().getSession();
		TbUser user_login = (TbUser) session.get("user_login");
		
		return this.userDao.deleteContact(user_login, contactUser);
	}
	
	@Override
	public List<TbUser> getContects(TbUser user) {
		return this.userDao.getContacts(user);
	}
	
	@Override
	public TbUser getUserById(String userId) {
		
		return this.userDao.getUser(userId);
	}

	@Override
	public boolean isExist(String userId) {
		TbUser user = this.getUserById(userId);
		if(user != null){
			return true;
		}else{
			return false;
		}
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public CommonDao getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	

	

	
	
	
	
	
}